using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Auto.Data.Setting;

using Auto.Data.HumanResource;

using System.Reflection;
namespace BaseSystem._System
{

    public partial class UserRolesSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        #region Init Page
        private void InitPage()
        {

            GVUsers.Attributes.Add("SortExpression", "UserID");
            GVUsers.Attributes.Add("SortDirection", " ASC");
            BindGridView();
            this.UcRolesTree1.TreeInit("", "");

        }
        #endregion Init Page

        #region Data Bind


        private void BindGridView()
        {
            DataTable dt = new Auto.Data.Security.Users().GetDataByKey(this.txtUserID.Text, "", this.UcDivision1.DivID);
            string sortExpression = this.GVUsers.Attributes["SortExpression"];
            string sortDirection = this.GVUsers.Attributes["SortDirection"];
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dt.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            GVUsers.DataSource = dt;
            GVUsers.DataBind();
        }

        #endregion Data Bind

        #region Event
        protected void GVUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "SetRoles":
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    string userID = ((Label)GVUsers.Rows[rowIndex].FindControl("lblUserID")).Text; //GVUsers.Rows[rowIndex].Cells[3].Text;
                    System.Drawing.Color selectedbackcolor = System.Drawing.ColorTranslator.FromHtml("#d7e3f6");
                    for (int i = 0; i < GVUsers.Rows.Count; i++)
                    {
                        if (i == rowIndex)
                        {
                            GVUsers.Rows[i].BackColor = selectedbackcolor;
                        }
                        else
                        {
                            GVUsers.Rows[i].BackColor = System.Drawing.Color.Empty;
                        }
                    }
                    try
                    {
                        tbRoles.Visible = true;
                        this.lblUserName.Text = userID;
                        UsersRoles userRoles = new UsersRoles(userID);
                        IDictionary<string, string> checkNode = new Dictionary<string, string>();
                        foreach (UsersRoles roles in userRoles.Items)
                        {
                            if (!checkNode.ContainsKey(roles.RoleID + "|" + roles.SysID))
                            {
                                checkNode.Add(roles.SysID + "|" + roles.RoleID, roles.WithGrant.ToString());
                            }
                        }

                        this.UcRolesTree1.CheckedNodes = checkNode;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    break;
                
            }


        }
        protected void GVUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVUsers.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        protected void GVUsers_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpress = e.SortExpression.ToString();

            //string[] s = sortExpress.Split('.');
            //string cl = "Auto.Data.HumanResource." + s[0];
            //object moduleobject = Assembly.Load("Auto.Data.Security").CreateInstance(cl);
            //string f = moduleobject.GetType().GetField(s[1]).GetValue(moduleobject).ToString();

            string sortDirection = this.GVUsers.Attributes["SortDirection"].ToString();
            GVUsers.Attributes["SortExpression"] = sortExpress;
            GVUsers.Attributes["SortDirection"] = sortDirection == " DESC" ? " ASC" : " DESC";


            BindGridView();
        }

        protected void lkbtnOK_Click(object sender, EventArgs e)
        {
            UsersRoles usersRoles = new UsersRoles();
            usersRoles.Delete(this.lblUserName.Text);

            IDictionary<string, string> checkValues = this.UcRolesTree1.CheckedNodes;
            if (checkValues != null && checkValues.Count > 0)
            {
                foreach (KeyValuePair<string, string> keyValue in checkValues)
                {
                    string[] roleSys = keyValue.Key.Split('|');
                    usersRoles = new UsersRoles();
                    usersRoles.RoleID = roleSys[0];
                    usersRoles.SysID = roleSys[1];
                    usersRoles.UserID = this.lblUserName.Text;
                    usersRoles.WithGrant = Convert.ToBoolean(keyValue.Value);
                    if (!usersRoles.Exists(this.lblUserName.Text, usersRoles.SysID, usersRoles.RoleID))
                    {
                        usersRoles.Insert();
                    }
                }
            }
            this.tbRoles.Visible = false;
        }
        protected void lkbtnCancel_Click(object sender, EventArgs e)
        {
            this.tbRoles.Visible = false;
        }

        protected void ddlDiv_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void ddlEmpID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView();
        }
        #endregion Event





    }
}
